const Service = require('egg').Service;
class UserService extends Service {
    /**
     * 获取User模型
     * 
     * 此方法用于从当前应用程序上下文中获取User模型的引用
     * 它提供了一种通过访问当前实例来获取User模型实例的简便方式
     * 
     * @returns {Object} User模型的实例
     */
    get User() {
        return this.app.model.User;
    }
    get Subscribe() {
        return this.app.model.Subscribe
    }

    /**
     * 异步注册用户
     * @param {Object} user - 用户对象，包含用户名和密码等信息
     * @returns {Promise} - 返回用户模型保存的结果
     */
    async registUser(user) {
        // 对用户密码进行md5加密处理，以确保密码安全
        user.password = this.ctx.helper.md5(user.password);
        // 创建新的用户模型实例
        const usermodel = new this.User(user);
        // 保存用户模型到数据库，并等待操作完成
        const dbback = await usermodel.save();
        const date = JSON.parse(JSON.stringify(dbback))
        delete date.password;
        // 返回数据库操作结果
        return date;
    }

    async findaccount(account) {
        const user = await this.User.findOne({ account });
        return user;
    }

    async findemail(email) {
        const user = await this.User.findOne({ email });
        return user;
    }

    async findphone(phone) {
        const user = await this.User.findOne({ phone });
        return user;
    }
    /**
     * 获取用户列表
     * @param {*} page 
     * @param {*} pagesize 
     * @returns 
     */
    async getUserList(page,pagesize){
        const skip = (page - 1)*pagesize
        const dbback = await this.User.find().skip(skip).limit(pagesize)
        const date = JSON.parse(JSON.stringify(dbback))
        const count = await this.User.countDocuments()
        date.forEach(item=>{
            delete item.password
        })
        return {
            list:date,
            count
        }
    }
    /**
     * 修改数据库的信息
     */
    async updateUser(id,userinfo){
        const dbback = await this.User.findByIdAndUpdate(id,userinfo,{new:true})
        return dbback
    }

    /**
     * 获取用户详情数据
     */
    async getUserInfo(id){
        const dbback = await this.User.findById(id)
        return dbback
    }

    async subscribe(userid,subscribeid){
        const subscribe = new this.Subscribe({userid,subscribeid})
        await subscribe.save()
    }

    async dissubscribe(userid,subscribeid){
        const dbback = await this.Subscribe.findOneAndDelete({userid,subscribeid})
        return dbback
    }

    async getSubscribe(userid,subscribeid){
        const dbback = await this.Subscribe.findOne({userid,subscribeid})
        return dbback
    }

    async getSubscribeList(page,pagesize,userid){
        const skip = (page - 1)*pagesize
        const dbback = await this.Subscribe.find({userid})
        .skip(skip)
        .limit(pagesize)
        .populate('subscribeid',' _id  account nickname subscribecount')
        const date = JSON.parse(JSON.stringify(dbback))
        const count = await this.Subscribe.countDocuments({userid})
        date.forEach(item=>{
            delete item.password
        })
        return {
            list:date,
            count
        }
    }

    async getFansList(page,pagesize,subscribeid){
        const skip = (page - 1)*pagesize
        const dbback = await this.Subscribe.find({subscribeid})
        .skip(skip)
        .limit(pagesize)
        .populate('userid',' _id  account nickname subscribecount')
        const date = JSON.parse(JSON.stringify(dbback))
        const count = await this.Subscribe.countDocuments({subscribeid})
        date.forEach(item=>{
            delete item.password
        })
        return {
            list:date,
            count
        }
    }
}

module.exports = UserService;